<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>

    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" />
    </el-form-item>

    <el-form-item label="昵称" prop="userNikename">
      <el-input v-model="ruleForm.userNikename" />
    </el-form-item>

    <el-form-item label="序号" prop="number">
      <el-input v-model="ruleForm.number" />
    </el-form-item>
    
    <el-form-item label="角色" prop="roleName">
      <el-select v-model="ruleForm.roleName" placeholder="请选择角色">
        <el-option label="教师" value="教师" />
        <el-option label="学生" value="学生" />
        <el-option label="boos" value="boos" />
      </el-select>
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        保存
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import {wrire} from '../Until/axiosHelp'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

interface RuleForm {
  "userName": "",
  "password": "",
  "userNikename": "",
  "number": "",
  "roleName": ""
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  "userName": "",
  "password": "",
  "userNikename": "",
  "number": "",
  "roleName": ""
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  userName: [
    { required: true, message: '用户不能为空', trigger: 'blur' },
    
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'change',
    },
  ],
    userNikename: [
    {
      required: true,
      message: '昵称不能为空',
      trigger: 'change',
    },
  ],
    number: [
    {
      required: true,
      message: '序号不能为空',
      trigger: 'change',
    },
  ],
    roleName: [
    {
      required: true,
      message: '角色不能为空不能为空',
      trigger: 'change',
    },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      wrire.post("api/Query/UserAdd",ruleForm).then(res=>{
        if(res.data.code==200)
      {
        ElMessage.success(res.data.msg)
      }
      else{
        ElMessage.error(res.data.msg)
      }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>
